package com.shameit.goodqrinfomanager;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.shameit.goodqrinfomanager.source.http.DataRepository;
import com.shameit.goodqrinfomanager.source.http.Injection;
import com.shameit.goodqrinfomanager.ui.activity.actionlog.ActionLogViewModel;
import com.shameit.goodqrinfomanager.ui.activity.barcode.BarCodeViewModel;
import com.shameit.goodqrinfomanager.ui.activity.cangku.CangkuManagementViewModel;
import com.shameit.goodqrinfomanager.ui.activity.chuku.ChukuViewModel;
import com.shameit.goodqrinfomanager.ui.activity.login.LoginViewModel;
import com.shameit.goodqrinfomanager.ui.activity.main.MainViewModel;
import com.shameit.goodqrinfomanager.ui.activity.offline.OfflineManagementViewModel;
import com.shameit.goodqrinfomanager.ui.activity.query.InteQueryViewModel;
import com.shameit.goodqrinfomanager.ui.activity.setting.BaseicSettingActivity;
import com.shameit.goodqrinfomanager.ui.activity.setting.BaseicSettingViewModel;
import com.shameit.goodqrinfomanager.ui.activity.sync.DataSyncViewModel;
import com.shameit.goodqrinfomanager.ui.activity.sync.SyncLogViewModel;

public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final DataRepository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, DataRepository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(MainViewModel.class)) {
            return (T) new MainViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ActionLogViewModel.class)) {
            return (T) new ActionLogViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(BaseicSettingViewModel.class)) {
            return (T) new BaseicSettingViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(BarCodeViewModel.class)) {
            return (T) new BarCodeViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(DataSyncViewModel.class)) {
            return (T) new DataSyncViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(InteQueryViewModel.class)) {
            return (T) new InteQueryViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(OfflineManagementViewModel.class)) {
            return (T) new OfflineManagementViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(CangkuManagementViewModel.class)) {
            return (T) new CangkuManagementViewModel(mApplication,mRepository);
        } else if (modelClass.isAssignableFrom(SyncLogViewModel.class)) {
            return (T) new SyncLogViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ChukuViewModel.class)) {
            return (T) new ChukuViewModel(mApplication, mRepository);
        }


        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
